// pages/packageA/renewDetail/renewDetail.js
var app = getApp();
var util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX: app.globalData.isIphoneX,
    id: '',
    formPage: '',
    detail: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.info('==== options ====', options)
    this.setData({
      id: options.id,
      formPage: options.formPage
    })
    this.getDetail()
  },

  getDetail() {
    wx.get(`${wx.api.renewDetail}?id=${this.data.id}`, res => {
      console.log('==== renewDetail ===', res)
      res.data.detail = res.data.detail.replace(/\/\>/gi, ' style="display:block" width="100%" height=""/>')
      this.setData({
        detail: res.data
      })
    }, fail => {})
  },

  agree: util.throttle(function(e) {
    let api = wx.api.renewAgree
    if (this.data.detail.isAgree == 1) api = wx.api.renewDeleteAgree
    wx.post(`${api}${this.data.id}`, {}, res => {
      console.log('==== agree ===', res)
      if (this.data.detail.isAgree == 1) {
        this.setData({
          ['detail.isAgree']: 0
        })
      } else {
        this.setData({
          ['detail.isAgree']: 1
        })
      }
      wx.showToast({
        title: this.data.detail.isAgree == 1 ? '谢谢支持' : '已取消',
        icon: 'none',
        duration: 1000
      })
    }, fail => {})
  }, 3000),

  goDetail() {
    let delta = 1
    if (this.data.formPage  == 'renew') delta = 2
    wx.navigateBack({
      delta
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})